﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;

using System.Windows.Forms;

using DotSpatial.Topology;

using System.Drawing.Drawing2D;



namespace FXnaGis
{

    /// <summary>
    /// 具有大气特征的矩形,就是具有旋转角的正方形
    /// </summary>
    public class FRectMF:FMapFunction
    {

        public FRectMF(FMapFrame frame)
            : base(frame)
        {

            this._enabled = false;
            

        }


        //用于存储一系列的点

        List<Coordinate> _coords = new List<Coordinate>();


        Coordinate _firstCoord;

        LineSegment _line;

        Coordinate _secondCoord;

        Coordinate _lastCoord;

        double _angle;

        int _count = 0;



        System.Drawing.Point _mousePosition;


        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }


        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            //保证count 从 1 到 3

            if (this._count < 3)
            {
                this._count += 1;

            }
            else
            {
                this._count = 1;
            }


            if (e.Button==MouseButtons.Left)
            {

                switch (this._count)
	            {
                    case 1:

                         this._firstCoord = this._geoMapFrame.BufferToProj(e.Location);

                        break;


                    case 2:


                        //需要计算角度


                        Coordinate c = this._geoMapFrame.BufferToProj(e.Location);

                        this._secondCoord = c;

                       

                       this._line = new LineSegment(this._firstCoord,c);

                        this._angle = this._line.Angle/Math.PI*180;

                       


                        break;

                    case 3:

                        this._lastCoord = this._geoMapFrame.BufferToProj(e.Location);

                        break;


                

		            default:
             break;
	            }


               
            }



        }


        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);


            //正在处理第几个点

            if (this._count==1||this._count==2)
            {
                _mousePosition = e.Location;

                this._geoMapFrame.Parent.Invalidate();

                
            }

            

        }


        protected override void OnDraw(FMapDrawArgs e)
        {
            base.OnDraw(e);


            switch (this._count)
            {

                    //画直线

                case 1:

                    e.Graphics.DrawLine(Pens.LightBlue,this._geoMapFrame.ProjToBuffer(this._firstCoord),this._mousePosition);

                    break;


                    //画矩形
                case 2:


                    Coordinate c = this._geoMapFrame.BufferToProj(this._mousePosition);

                    double ylength = this._line.DistancePerpendicular(c);

                    double xylength = c.Distance(this._firstCoord);

                    double xlength = Math.Sqrt(xylength*xylength-ylength*ylength);

                    double width=this._geoMapFrame.ProjToBuffer(xlength);

                    double height= this._geoMapFrame.ProjToBuffer(ylength);



                    System.Drawing.Point pt = this._geoMapFrame.ProjToBuffer(this._firstCoord);

                    Rectangle rectangle = new Rectangle(pt.X,pt.Y,(int)width,(int)height);

                    Matrix org = e.Graphics.Transform;

                    Matrix rotate = org.Clone();

                    PointF pf = new PointF(pt.X,pt.Y);



                    //由于这里是旋转坐标系，坐标系是按照顺时针旋转的，这样就造成了我们绘制的临时图形，是逆时针旋转，所以这里的角度需要特殊考虑

                    double nAngle =360- this._angle;

                    rotate.RotateAt((float)nAngle, pt, MatrixOrder.Append);

                   

                    e.Graphics.Transform = rotate;

                    e.Graphics.DrawRectangle(Pens.LightBlue, rectangle);


                    e.Graphics.Transform = org;


                    break;


               


                default:
                    break;
            }
        }



        

      



    }
}
